package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description
 * User:华为
 * Date:2024/10/18
 * Time:21:10
 */
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你想删除的图书");
        String name = scanner.nextLine();
        int pos = -1;

        int currentsize = bookList.getUsedSize();
        for (int i = 0; i < currentsize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                pos = i;
                break;
            }
        }
        if(pos == -1){
            System.out.println("未找到要删除的图书");
            return;
        }
        //开始删除
        int j = pos;
        for (; j <currentsize-1 ; j++) {
            //[j]=[j+1]  此刻booklist是一个类型不是一个数组不能像这样直接赋值
            Book book = bookList.getBook(j+1);
            bookList.setBooks(j,book);
        }
        bookList.setBooks(j,null);

        bookList.setUsedSize(currentsize-1);

    }
}
